/*
 @file main.c
 @author Paul Hubbard
 @note code starting from excellent Apple base at http://developer.apple.com/hardwaredrivers/customusbdrivers.html
 @note Project page at http://code.google.com/p/omron-osx-driver/
 */

#include <stdio.h>
#include <IOKit/IOCFPlugIn.h>
#include <IOKit/hid/IOHIDKeys.h>
#include <CoreFoundation/CoreFoundation.h>

// Structure for USB report from device - buffer may be wrong size!
/*
struct reader 
{
	io_object_t ioObject;
	IOHIDDeviceInterface **interface;
	int gotdata;
	unsigned char buffer[64];
};

//! Global variables
SInt32 score;
IOCFPlugInInterface **plugInInterface;
CFRunLoopSourceRef eventSource;
mach_port_t port;
struct reader *r;
*/

// Defines
const int ourVendor = 0x0590;
const int ourDevice = 0x0028;

// Main routine
int main(void) 
{
	IOReturn result = kIOReturnSuccess;
	io_iterator_t hidObjectIterator = 0;
	io_object_t hidDevice = IO_OBJECT_NULL;
	CFMutableDictionaryRef hidMatchDictionary = 0;
    CFMutableDictionaryRef hidProperties = 0;
    boolean_t  omFound = FALSE;
    
    hidMatchDictionary = IOServiceMatching(kIOHIDDeviceKey);
    result = IOServiceGetMatchingServices(kIOMasterPortDefault,
                                          hidMatchDictionary,
                                          &hidObjectIterator);
    if ((result != kIOReturnSuccess) || (hidObjectIterator == 0)) 
    {
        printf("Can't obtain an IO iterator\n");
        exit(1);
    }
    
    // Iterate over HID devices attached to the system
    while ((hidDevice = IOIteratorNext(hidObjectIterator))) 
    {
        hidProperties = 0;
        int vendor = 0, product = 0;
        result = IORegistryEntryCreateCFProperties(hidDevice, &hidProperties,
                                                   kCFAllocatorDefault, kNilOptions);
        if ((result == KERN_SUCCESS) && hidProperties) 
        {
            CFNumberRef vendorRef, productRef;
            
            vendorRef = CFDictionaryGetValue(hidProperties, CFSTR(kIOHIDVendorIDKey));
            productRef = CFDictionaryGetValue(hidProperties, CFSTR(kIOHIDProductIDKey));
            if (vendorRef) 
            {
                CFNumberGetValue(vendorRef, kCFNumberIntType, &vendor);	   
                CFRelease(vendorRef);
            }
            if (productRef) 
            {
                CFNumberGetValue(productRef, kCFNumberIntType, &product);
                CFRelease(productRef);
            }
            if((vendor == ourVendor) && (product == ourDevice))
            {
                omFound = TRUE;
                
                // Hook work goes here?
                IOObjectRelease(hidDevice);
                break;
            }
        }
//        printf("Got a device: vendor %04x, product %04x\n", vendor, product);
        IOObjectRelease(hidDevice);
    }
    // After while loop, release iterator
	IOObjectRelease(hidObjectIterator);
    
    if(!omFound)
    {
        printf("\nOmron not located, exiting.\n");
        return(1);
    }
    
    printf("\nOmron found!!\n");
    
	return 0;
}


